package com.example.SpringBoot_demo.controller;

import com.example.SpringBoot_demo.bean.Notice;
import com.example.SpringBoot_demo.service.INoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

/**
 * Creat by wsn on 2021/7/21 10:42
 */
@Controller
public class NoticeController {
    @Autowired
    private INoticeService noticeService;
    private int curPage=1;

//    分页和查看公告首页
    @GetMapping("noticePage.do")
    public String noticePage(String firstPage,String lastPage,String nextPage,String finalPage, HttpServletRequest request){
        int pageSize=6;
        int totalPage=noticeService.total(6);
        if (firstPage != null){//首页
            curPage = 1;
        }
        if (lastPage != null){//上一页
            curPage--;
        }
        if (nextPage != null){//下一页
            curPage++;
        }
        if (finalPage != null){//尾页
            curPage = totalPage;
        }
        if (curPage < 1){//确定下届
            curPage = 1;
        }
        if (curPage > totalPage){//确定下届
            curPage = totalPage;
        }
        //把数据准备好，放入request作用域
        int startRow = (curPage-1)*pageSize;//当前页的第一条数据在数据库的位置
        request.setAttribute("notices",noticeService.GetAllnotice(startRow,6));//notices与前端模板内容对应
        System.out.println("成功获取页面！！！");
//        删除公告

        return "noticePage";
    }
//单个删除数据
    @GetMapping("/delete/{id}")//ref风格注意对应，前端与controller
    public String delete(@PathVariable Integer id,Notice notice, HttpServletRequest request){
        System.out.println(("删除成功,所删公告ID：")+id);
        noticeService.delete(id);
//        刷新公告ID
        noticeService.freshAllNoticeID1(notice);
        noticeService.freshAllNoticeID2(notice);
        return "redirect:/noticePage.do";
    }

    //批量删除数据
    @RequestMapping("/deleteSum/{ids}")//ids为数组
    public ModelAndView delete(@PathVariable String ids,Notice notice){
        String[] strs = ids.split(",");
        for (int i = 0; i < strs.length; i++) {
            try {
                noticeService.delete(Integer.parseInt(strs[i]));
            } catch (Exception e) {
            }
        }
        System.out.println("正在执行批量删除！");
        //        刷新公告ID
        noticeService.freshAllNoticeID1(notice);
        noticeService.freshAllNoticeID2(notice);
        return new ModelAndView("redirect:/noticePage.do");
    }

//修改公告
    @RequestMapping("/update")//获取到提交表单后的数据
    public String update(Notice  notice){
        noticeService.update(notice);
        System.out.println(notice);
        return "redirect:/noticePage.do";
    }

//    添加公告
    @RequestMapping("/addNotice/{text}")
    public String addNotice(@PathVariable String text, Notice notice, HttpSession session){
        System.out.println(text);

        Object userid1=session.getAttribute("userId");
        String userId=userid1.toString();
        System.out.println("userId:" + userId);
        notice.setAdminid(userId);

        Object userName1=session.getAttribute("userName");
        String userName=userName1.toString();
        System.out.println("userName:" + userName);
        notice.setAdminname(userName);

        notice.setNoticedate(new Timestamp(System.currentTimeMillis()));
        notice.setNoticecontents(text);

        System.out.println(notice);

        noticeService.noticeinsert(notice.getAdminid(),notice.getAdminname(),notice.getNoticecontents(),notice.getNoticedate());


        return "redirect:/noticePage.do";
    }


//    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式，不显示毫秒
//    Timestamp now = new Timestamp(System.currentTimeMillis());//获取系统当前时间
//    String str = df.format(now);
//        System.out.println(str);




}
